import axios from 'axios'


const request = axios.create({
  baseURL: "/api",
  timeout: 10000,
  // withCredentials: true
})
const messages = {
  401: '禁止访问',
  403: '无权访问',
  404: '找不到页面',
  500: '服务器错误',
  301: "需要登录"
};
// 请求拦截器
request.interceptors.request.use((config) => {
  // 设置token
  // if (store.state.user.token) {
  //   config.headers.token = store.state.user.token
  // }
  config.headers.token = "MUSIC_U=f1ee9c1cdd5faa029ac4632790e3e07fcf5c1edd06d12395c4b7a65a708bbf3cd106471d18c1d4a4; Max-Age=1296000; Expires=Wed 4 Aug 2021 12:59:58 GMT; Path=/;"
  return config
});

request.interceptors.response.use((response) => {

  // console.log('utils',response)
  if (response.data.code === 200) {
    return response.data
  } else {
    return Promise.reject(response.data.message || '未知错误,请联系管理员')
  }
},
  (error) => {

  
    let message = '未知错误,请联系管理员'
    if (error.response) {
      message = messages[error.response.status]
    } else {
      if (error.message.indexOf('timeout') > -1) {
        message = '响应超时,网速太慢'
      } else if (error.message.indexOf('Network') > -1) {
        message = '没有网络'
      }
    }
    return Promise.reject(message)
  });

export default request;
